# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=virt-viewer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.0
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="Displaying the graphical console of a virtual machine (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-spice-gtk"
         "${MINGW_PACKAGE_PREFIX}-gtk-vnc"
         "${MINGW_PACKAGE_PREFIX}-libvirt"
         "${MINGW_PACKAGE_PREFIX}-libvirt-glib"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-opus")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-icoutils"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
             "${MINGW_PACKAGE_PREFIX}-spice-protocol"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' 'staticlibs')
license=("LGPL 2.1")
url="https://virt-manager.org/"
source=(https://virt-manager.org/download/sources/virt-viewer/virt-viewer-${pkgver}.tar.xz{,.asc}
        001-rc-generation-fixes.patch
        002-check-linker-pie-support.patch
        003-fix-arm64-link.patch)
sha256sums=('a43fa2325c4c1c77a5c8c98065ac30ef0511a21ac98e590f22340869bad9abd0'
            'SKIP'
            'ef8d364e7f05c799ffd98c798f45dd125ab90d1a5fc116052e9e8b769182a03e'
            '6a52d3807518536ec605dfd331d8ab0c993ca49d147d84b3e71f0ca0ae9f41dd'
            '0bbacaaa3a002fe609aae9751f0930754b9a2b045c48136a041a707525f5e344')
validpgpkeys=('DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF') # Daniel P. Berrange

prepare() {
  cd $srcdir/${_realname}-${pkgver}

  patch -Nbp1 -i "${srcdir}/001-rc-generation-fixes.patch"
  patch -Nbp1 -i "${srcdir}/002-check-linker-pie-support.patch"
  patch -Nbp1 -i "${srcdir}/003-fix-arm64-link.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=disabled \
    -Dlibvirt=enabled \
    -Dvnc=enabled \
    -Dspice=enabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "$srcdir/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
